package it.polimi.reTravelDream.beans;


import it.polimi.reTravelDream.dto.ExcursionDTO;
import it.polimi.reTravelDream.dto.FlightDTO;
import it.polimi.reTravelDream.dto.HotelDTO;
import it.polimi.reTravelDream.dto.PrenotationDTO;
import it.polimi.reTravelDream.dto.SearchPackageDTO;
import it.polimi.reTravelDream.dto.TravelPackageDTO;
import it.polimi.reTravelDream.dto.UserDTO;
import it.polimi.reTravelDream.eao.PrenotationEAOLocal;
import it.polimi.reTravelDream.eao.TravelPackageActionEAOLocal;
import it.polimi.reTravelDream.eao.UserEAOLocal;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;


@ManagedBean(name="searchBar")
@SessionScoped
public class SearchBar {
	
	private boolean isShowResult;
	private String noResult;
	private List<TravelPackageDTO> allPackageDTOs;
	private boolean isAllResult;
	private String errorSearchEmpty;
	
	private TravelPackageDTO packageSelecteDto;
	private List<FlightDTO> flightRelated;
	private List<HotelDTO> hotelRelated;
	private List<ExcursionDTO> excursionRelated;
	
	private FlightDTO outGoingFlight;
	private FlightDTO returnFlight;
	
	private HashMap<Integer, FlightDTO> flightRelaHashMap;
	
	private HotelDTO hotelChosed;
	
	private boolean isSelected;
	
	private boolean isOutgoing;
	
	private boolean isReturn;
	
	private boolean isHotel;
	
	private ExcursionDTO excursionChosed;
	private boolean isExcursion;
	
	private boolean isRecap;
	
	private String userName;
	
	private UserDTO currentUser;
	
	private String prenotationOK;
	
	private String confirmPrenotation;
	private boolean isPrenotationConfirmed;
	

	@EJB
	private TravelPackageActionEAOLocal packageAction;
	
	@EJB
	private UserEAOLocal userEAOLocal;
	
	@EJB
	private PrenotationEAOLocal prenotationEAOLocal;
	
	private SearchPackageDTO searchPackageDTO;
	private List<TravelPackageDTO> resultPackageQuery;
	
	private PrenotationDTO prenotationDTO;
	

	public SearchBar(){
		this.excursionChosed=new ExcursionDTO();
		
		this.currentUser=new UserDTO();
		
		this.prenotationDTO=new PrenotationDTO();
		
		this.flightRelaHashMap=new HashMap<Integer,FlightDTO>();
		this.hotelChosed=new HotelDTO();
		this.flightRelated=new ArrayList<FlightDTO>();
		this.hotelRelated=new ArrayList<HotelDTO>();
		this.excursionRelated=new ArrayList<ExcursionDTO>();
		
		this.outGoingFlight=new FlightDTO();
		this.returnFlight=new FlightDTO();
		
		this.packageSelecteDto=new TravelPackageDTO();
		this.isAllResult=false;
		this.allPackageDTOs=new ArrayList<TravelPackageDTO>();
		this.isShowResult=false;
		this.searchPackageDTO=new SearchPackageDTO();
		this.resultPackageQuery=new ArrayList<TravelPackageDTO>();
	}

	@PostConstruct
	public void allPackages(){
		this.allPackageDTOs=this.packageAction.getAllPackages();
		String email=FacesContext.getCurrentInstance().getExternalContext().getRemoteUser();
		this.currentUser=this.userEAOLocal.getUser(email);
	}
	
	public void search(){
		this.isPrenotationConfirmed=false;

		this.errorSearchEmpty="";
		this.noResult="";


		
		if(this.searchPackageDTO.getKeyWord().isEmpty()){
			this.errorSearchEmpty="you must insert a word";
			this.isAllResult=false;
			this.isSelected=false;
			this.isShowResult=true;
			return;
		}
		this.resultPackageQuery=this.packageAction.searchPackage(this.searchPackageDTO);
		
		if (this.resultPackageQuery.isEmpty()) {
			this.isShowResult=false;
			this.noResult="no result";
			return;
		}
		this.isAllResult=false;
		this.isSelected=false;
		this.isShowResult=true;
	}
	

	public void selectPackage(TravelPackageDTO packageSelected){
		this.packageSelecteDto=packageSelected;
		this.flightRelated=packageSelected.getPackageFlights();
		this.hotelRelated=packageSelected.getPackageHotelDTOs();
		this.excursionRelated=packageSelected.getPackageExcursions();
		
		for(FlightDTO flightDTO:this.flightRelated){
			this.flightRelaHashMap.put(flightDTO.getId(), flightDTO);	
		}
		this.isSelected=true;
		this.isOutgoing=true;
		this.errorSearchEmpty="";
		this.noResult="";

	}
	
	public void showAllPackage(){
		this.isPrenotationConfirmed=false;

		this.errorSearchEmpty="";
		this.noResult="";
		this.searchPackageDTO=new SearchPackageDTO();
		this.isAllResult=true;
		this.isSelected=false;
		this.isShowResult=false;
		
	}
	
	public void addOutGoingFlight(FlightDTO outBoundFlight){

		this.outGoingFlight=outBoundFlight;
		this.flightRelaHashMap.remove(outBoundFlight.getId());
		this.flightRelated.clear();
		
		for(Integer i:this.flightRelaHashMap.keySet()){
			this.flightRelated.add(this.flightRelaHashMap.get(i));
		}	
		this.isOutgoing=false;
		this.isReturn=true;
		
	}
	
	public void returnFlight(FlightDTO returnFlight){
	
		this.returnFlight=returnFlight;
		this.hotelRelated=this.packageSelecteDto.getPackageHotelDTOs();
		this.isHotel=true;
		this.isReturn=false;
	}
	
	public void addHotel(HotelDTO hotelChosed){
	


		this.hotelChosed=hotelChosed;

		this.excursionRelated=this.packageSelecteDto.getPackageExcursions();
		this.isHotel=false;
		this.isExcursion=true;

	}
	
	public void addExcursion(ExcursionDTO excursionToAdd){
		
		this.excursionChosed=excursionToAdd;
		this.isExcursion=false;
		this.isRecap=true;
		
		this.prenotationDTO.setClient(this.currentUser);
		this.prenotationDTO.setDepartureFlight(this.outGoingFlight);
		this.prenotationDTO.setReturnFlight(this.returnFlight);
		this.prenotationDTO.setHotelChosen(this.hotelChosed);
		this.prenotationDTO.setExcursioneChosen(this.excursionChosed);
		this.prenotationDTO.setPrice(this.calculatePrice());
		this.prenotationDTO.setPackageName(this.packageSelecteDto.getPackageName());
		
	}
	
	private int calculatePrice(){
		int total;
		
		total=(this.outGoingFlight.getPrice()+this.returnFlight.getPrice()+
				(this.hotelChosed.getPrice()*this.packageSelecteDto.getNumberOfNights())+
				this.excursionChosed.getPrice())*this.packageSelecteDto.getNumberOfPerson();
				
		return total;
	}
	

	

	public void confirm(){
		this.isPrenotationConfirmed=true;
		this.prenotationEAOLocal.makeReservation(this.prenotationDTO);
		this.prenotationOK="prenotation successfull, you can go to your personal page to see te prenotation";
		
		this.isRecap=false;
		this.isAllResult=false;
		this.isSelected=false;
		this.isShowResult=false;
		this.excursionChosed=new ExcursionDTO();
		this.hotelChosed=new HotelDTO();
		this.outGoingFlight=new FlightDTO();
		this.returnFlight=new FlightDTO();
		this.packageSelecteDto=new TravelPackageDTO();
		this.prenotationDTO=new PrenotationDTO();
		this.isOutgoing=false;
		this.isReturn=false;
		this.isHotel=false;
		this.isExcursion=false;
		this.flightRelated.clear();
		this.hotelRelated.clear();
		this.excursionRelated.clear();
		
		this.excursionChosed=new ExcursionDTO();
		
		this.currentUser=new UserDTO();
		
		this.prenotationDTO=new PrenotationDTO();
		
		this.flightRelaHashMap=new HashMap<Integer,FlightDTO>();
		this.hotelChosed=new HotelDTO();
		this.flightRelated=new ArrayList<FlightDTO>();
		this.hotelRelated=new ArrayList<HotelDTO>();
		this.excursionRelated=new ArrayList<ExcursionDTO>();
		
		this.outGoingFlight=new FlightDTO();
		this.returnFlight=new FlightDTO();
		
		this.packageSelecteDto=new TravelPackageDTO();
		this.isAllResult=false;
		this.allPackageDTOs=new ArrayList<TravelPackageDTO>();
		this.isShowResult=false;
		this.searchPackageDTO=new SearchPackageDTO();
		this.resultPackageQuery=new ArrayList<TravelPackageDTO>();
		
		this.allPackageDTOs=this.packageAction.getAllPackages();
		String email=FacesContext.getCurrentInstance().getExternalContext().getRemoteUser();
		this.currentUser=this.userEAOLocal.getUser(email);
	}
	
	public void cancel(){
		this.isRecap=false;
		this.isPrenotationConfirmed=false;
		this.isAllResult=false;
		this.isSelected=false;
		this.isShowResult=false;
		this.excursionChosed=new ExcursionDTO();
		this.hotelChosed=new HotelDTO();
		this.outGoingFlight=new FlightDTO();
		this.returnFlight=new FlightDTO();
		this.packageSelecteDto=new TravelPackageDTO();
		this.prenotationDTO=new PrenotationDTO();
		this.isOutgoing=false;
		this.isReturn=false;
		this.isHotel=false;
		this.isExcursion=false;
		this.flightRelated.clear();
		this.hotelRelated.clear();
		this.excursionRelated.clear();
		
		this.excursionChosed=new ExcursionDTO();
		
		this.currentUser=new UserDTO();
		
		this.prenotationDTO=new PrenotationDTO();
		
		this.flightRelaHashMap=new HashMap<Integer,FlightDTO>();
		this.hotelChosed=new HotelDTO();
		this.flightRelated=new ArrayList<FlightDTO>();
		this.hotelRelated=new ArrayList<HotelDTO>();
		this.excursionRelated=new ArrayList<ExcursionDTO>();
		
		this.outGoingFlight=new FlightDTO();
		this.returnFlight=new FlightDTO();
		
		this.packageSelecteDto=new TravelPackageDTO();
		this.isAllResult=false;
		this.allPackageDTOs=new ArrayList<TravelPackageDTO>();
		this.isShowResult=false;
		this.searchPackageDTO=new SearchPackageDTO();
		this.resultPackageQuery=new ArrayList<TravelPackageDTO>();
		
		this.allPackageDTOs=this.packageAction.getAllPackages();
		String email=FacesContext.getCurrentInstance().getExternalContext().getRemoteUser();
		this.currentUser=this.userEAOLocal.getUser(email);

	}
	
	
	/** getter and setter*/
	public SearchPackageDTO getSearchPackageDTO() {
		return searchPackageDTO;
	}

	public void setSearchPackageDTO(SearchPackageDTO searchPackageDTO) {
		this.searchPackageDTO = searchPackageDTO;
	}

	public List<TravelPackageDTO> getResultPackageQuery() {
		return resultPackageQuery;
	}

	public void setResultPackageQuery(List<TravelPackageDTO> resultPackageQuery) {
		this.resultPackageQuery = resultPackageQuery;
	}
	
	/**
	 * 
	 * getter and setter
	 */

	public boolean isShowResult() {
		return isShowResult;
	}

	public void setShowResult(boolean isShowResult) {
		this.isShowResult = isShowResult;
	}

	public String getNoResult() {
		return noResult;
	}

	public void setNoResult(String noResult) {
		this.noResult = noResult;
	}

	public List<TravelPackageDTO> getAllPackageDTOs() {
		return allPackageDTOs;
	}

	public void setAllPackageDTOs(List<TravelPackageDTO> allPackageDTOs) {
		this.allPackageDTOs = allPackageDTOs;
	}

	public boolean isAllResult() {
		return isAllResult;
	}

	public void setAllResult(boolean isAllResult) {
		this.isAllResult = isAllResult;
	}

	public String getErrorSearchEmpty() {
		return errorSearchEmpty;
	}

	public void setErrorSearchEmpty(String errorSearchEmpty) {
		this.errorSearchEmpty = errorSearchEmpty;
	}

	public boolean isSelected() {
		return isSelected;
	}

	public void setSelected(boolean isSelected) {
		this.isSelected = isSelected;
	}

	public TravelPackageDTO getPackageSelecteDto() {
		return packageSelecteDto;
	}

	public void setPackageSelecteDto(TravelPackageDTO packageSelecteDto) {
		this.packageSelecteDto = packageSelecteDto;
	}

	public List<FlightDTO> getFlightRelated() {
		return flightRelated;
	}

	public void setFlightRelated(List<FlightDTO> flightRelated) {
		this.flightRelated = flightRelated;
	}

	public List<HotelDTO> getHotelRelated() {
		return hotelRelated;
	}

	public void setHotelRelated(List<HotelDTO> hotelRelated) {
		this.hotelRelated = hotelRelated;
	}

	public List<ExcursionDTO> getExcursionRelated() {
		return excursionRelated;
	}

	public void setExcursionRelated(List<ExcursionDTO> excursionRelated) {
		this.excursionRelated = excursionRelated;
	}

	public FlightDTO getOutGoingFlight() {
		return outGoingFlight;
	}

	public void setOutGoingFlight(FlightDTO outGoingFlight) {
		this.outGoingFlight = outGoingFlight;
	}

	public FlightDTO getReturnFlight() {
		return returnFlight;
	}

	public void setReturnFlight(FlightDTO returnFlight) {
		this.returnFlight = returnFlight;
	}

	public boolean isOutgoing() {
		return isOutgoing;
	}

	public void setOutgoing(boolean isOutgoing) {
		this.isOutgoing = isOutgoing;
	}

	public boolean isReturn() {
		return isReturn;
	}

	public void setReturn(boolean isReturn) {
		this.isReturn = isReturn;
	}

	public HotelDTO getHotelChosed() {
		return hotelChosed;
	}

	public void setHotelChosed(HotelDTO hotelChosed) {
		this.hotelChosed = hotelChosed;
	}

	public boolean isHotel() {
		return isHotel;
	}

	public void setHotel(boolean isHotel) {
		this.isHotel = isHotel;
	}

	public ExcursionDTO getExcursionChosed() {
		return excursionChosed;
	}

	public void setExcursionChosed(ExcursionDTO excursionChosed) {
		this.excursionChosed = excursionChosed;
	}

	public boolean isExcursion() {
		return isExcursion;
	}

	public void setExcursion(boolean isExcursion) {
		this.isExcursion = isExcursion;
	}

	public boolean isRecap() {
		return isRecap;
	}

	public void setRecap(boolean isRecap) {
		this.isRecap = isRecap;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public UserDTO getCurrentUser() {
		return currentUser;
	}

	public void setCurrentUser(UserDTO currentUser) {
		this.currentUser = currentUser;
	}

	public PrenotationDTO getPrenotationDTO() {
		return prenotationDTO;
	}

	public void setPrenotationDTO(PrenotationDTO prenotationDTO) {
		this.prenotationDTO = prenotationDTO;
	}

	public String getPrenotationOK() {
		return prenotationOK;
	}

	public void setPrenotationOK(String prenotationOK) {
		this.prenotationOK = prenotationOK;
	}

	public String getConfirmPrenotation() {
		return confirmPrenotation;
	}

	public void setConfirmPrenotation(String confirmPrenotation) {
		this.confirmPrenotation = confirmPrenotation;
	}

	public boolean isPrenotationConfirmed() {
		return isPrenotationConfirmed;
	}

	public void setPrenotationConfirmed(boolean isPrenotationConfirmed) {
		this.isPrenotationConfirmed = isPrenotationConfirmed;
	}



}
